package com.lgx.mobile.dao.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.lgx.common.basic.dao.GeneralDao;
import com.lgx.common.basic.dao.GeneralJdbcDao;
import com.lgx.common.exception.DataException;
import com.lgx.common.web.Pagination;
import com.lgx.mobile.dao.IGroupChatRecordDao;
import com.lgx.mobile.entity.GroupChatRecord;
import com.lgx.mobile.vo.GroupChatRecordVO;

/**
 * 
 * @author xuyichen
 *
 */
@Component("groupChatRecordDao")
public class GroupChatRecordDao extends GeneralDao implements IGroupChatRecordDao {
	
	@Autowired
	private GeneralJdbcDao baseDao;
	
	public GeneralJdbcDao getBaseDao() {
		return baseDao;
	}

	public void setBaseDao(GeneralJdbcDao baseDao) {
		this.baseDao = baseDao;
	}

	protected Logger log = Logger.getLogger(this.getClass());
	
	public GroupChatRecord createGroupChatRecord(GroupChatRecord groupChatRecord) throws DataException {
		this.getHibernateTemplate().save(groupChatRecord);
		log.info("[创建圈子聊天记录]:"+groupChatRecord.getId());
		return groupChatRecord;
	}

	public GroupChatRecord updateGroupChatRecord(GroupChatRecord groupChatRecord) throws DataException {
		this.getHibernateTemplate().update(groupChatRecord);
		log.info("[更新圈子聊天记录]:"+groupChatRecord.getId());
		return groupChatRecord;
	}

	public void deleteGroupChatRecord(GroupChatRecord groupChatRecord) throws DataException {
		log.info("[删除圈子聊天记录]:"+groupChatRecord.getId());
		this.getHibernateTemplate().delete(groupChatRecord);
	}

	public void deleteGroupChatRecordById(long id) throws DataException {
		log.info("[删除圈子聊天记录]:"+id);
		this.deleteGroupChatRecord(this.findGroupChatRecordById(id));
	}

	public GroupChatRecord findGroupChatRecordById(long id) {
		return this.getHibernateTemplate().get(GroupChatRecord.class, id);
	}
	
	@SuppressWarnings("unchecked")
	public List<GroupChatRecord> findGroupChatRecordListByGroupId(long groupId){
		String sql = "from GroupChatRecord gcr where gcr.groupId = "+groupId+" order by gcr.id asc";
		return this.getHibernateTemplate().find(sql);
	}
	
	@SuppressWarnings("unchecked")
	public List<GroupChatRecord> findGroupChatRecordList(){
		String sql = "from GroupChatRecord gcr order by gcr.id asc";
		return this.getHibernateTemplate().find(sql);
	}

	@Override
	public Pagination findGroupChatRecordPaginationByGroupId(long groupId,Object[] params, int pageSize, int pageNum) {
		String sql = "select gcr.id as id," +
				"gcr.record_date as recordDate," +
				"gcr.content as content," +
				"gcr.group_id as groupId," +
				"gcr.user_id as userId," +
				"u.nickname as nickName," +
				"u.username as userName," +
				"u.truename as trueName";

		sql += " from tm_m_group_chat_record gcr left join ts_user u on u.id = gcr.user_id where u.isdelete = '0' ";
		
		sql += " and gcr.group_id = " + groupId;
		
		sql += " order by gcr.record_date desc";
		
		return baseDao.queryJdbcListPagination(sql, GroupChatRecordVO.class, pageSize, pageNum);
	}
}
